package com.example.mybatisplusdemo.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mybatisplusdemo.Methods.methods;
import com.example.mybatisplusdemo.Result.Result;
import com.example.mybatisplusdemo.Service.AdminService;
import com.example.mybatisplusdemo.Service.UserService;
import com.example.mybatisplusdemo.pojo.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api")
@CrossOrigin
@Validated
public class UserController {
    @Resource
    UserService userService;
    @Resource
    AdminService adminService;
    /**
     * 获取 user 全部列表
     * @param token
     * @return
     */
    @GetMapping("/getuser")
    public Result getuser(@RequestParam("token") String token){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        List<User> list = userService.list(new QueryWrapper<User>().orderByDesc("id"));
        return new Result(200,"获取成功",list);
    }

    /**
     * 按名查找 User
     * @param token
     * @param words
     * @return
     */
    @GetMapping("/getuserbyname")
    public Result getuserbyname(@RequestParam("token") String token,
                                @RequestParam("words") String words){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        List<User> list = userService.list(new QueryWrapper<User>().like("nike",words).orderByDesc("id"));
        return new Result(200,"查询成功",list);
    }

    /**
     * 添加 user
     * @param token
     * @param nike
     * @param loginname
     * @param pwd
     * @param tel
     * @param address
     * @return
     */
    @PostMapping("/adduser")
    public Result adduser(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                          @Valid @NotBlank(message = "昵称不能为空")@RequestParam("nike") String nike,
                          @Valid @NotBlank(message = "用户名不能为空")@RequestParam("loginname") String loginname,
                          @Valid @NotBlank(message = "密码不能为空")@RequestParam("pwd") String pwd,
                          @Valid @NotBlank(message = "电话不能为空")@RequestParam("tel") String tel,
                          @Valid @NotBlank(message = "地址不能为空")@RequestParam("address") String address
    ){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        if  (!new methods().checklenth(nike,3,10)) {return new Result(400,"昵称长度不正确");}
        if  (!new methods().checklenth(loginname,7,15)) {return new Result(400,"用户名长度不正确");}
        if  (!new methods().checklenth(pwd,6,15)) {return new Result(400,"密码长度不正确");}
        int count = userService.count(new QueryWrapper<User>().eq("loginname",loginname));
        if (count != 0){
            return new Result(400,"登录账号已经存在！");
        }
        User user = new User();
        user.setNike(nike);
        user.setLoginname(loginname);
        user.setPwd(new methods().md5(pwd));
        user.setTel(tel);
        user.setAddress(address);
        user.setToken(new methods().randomstr(14));
        boolean isSave = userService.save(user);
        if (isSave){return new Result(200,"添加成功");}
        else{return new Result(400,"添加失败");}
    }

    /**
     * 删除user
     * @param token
     * @param id
     * @return
     */
    @PostMapping("/deleteuser")
    public Result deleteuser(@RequestParam("token") String token,@RequestParam("id") String id){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        boolean isDelete = userService.remove(new QueryWrapper<User>().eq("id",id));
        if (isDelete){return new Result(200,"删除成功");}
        else{return new Result(400,"删除失败");}
    }


    /**
     * 更新方法
     * @param token
     * @param id
     * @param nike
     * @param tel
     * @param address
     * @return
     */
    @PostMapping("/updateuser")
    public Result updateuser(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                             @Valid @NotBlank(message = "id不能为空")@RequestParam("id") String id,
                             @Valid @NotBlank(message = "昵称不能为空")@RequestParam("nike") String nike,
                             @Valid @NotBlank(message = "电话不能为空")@RequestParam("tel") String tel,
                             @Valid @NotBlank(message = "地址不能为空")@RequestParam("address") String address){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        if  (!new methods().checklenth(nike,3,10)) {return new Result(400,"昵称长度不正确");}
        User user = userService.getOne(new QueryWrapper<User>().eq("id",id));
        user.setNike(nike);
        user.setTel(tel);
        user.setAddress(address);
        boolean isUpdate = userService.update(user,new QueryWrapper<User>().eq("id",id));
        if (isUpdate){return new Result(200,"更新成功");}
        else {return new Result(400,"更新失败");}
    }

    /**
     * 重置账号密码
     * @param token
     * @param id
     * @param loginname
     * @param pwd
     * @return
     */
    @PostMapping("/resetuser")
    public Result resetuser(@RequestParam("token") String token,
                            @RequestParam("id") String id,
                            @RequestParam("loginname") String loginname,
                            @RequestParam("pwd") String pwd){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        User user = userService.getOne(new QueryWrapper<User>().eq("id",id));
        if (!Objects.equals(user.getLoginname(), loginname)){
            int count = userService.count(new QueryWrapper<User>().eq("loginname",loginname));
            if (count != 0){return new Result(400,"登录账号已经存在");}
            user.setLoginname(loginname);
        }
        if (!Objects.equals(user.getPwd(), new methods().md5(pwd))){
            user.setPwd(new methods().md5(pwd));
        }
        boolean isUpdata = userService.update(user,new QueryWrapper<User>().eq("id",id));
        if (isUpdata){return new Result(200,"更新成功");}
        else{return new Result(400,"更新失败");}
    }

    @PostMapping("/register")
    public Result register(@Valid @NotBlank(message = "昵称不能为空")@RequestParam("nike") String nike,
                           @Valid @NotBlank(message = "用户名不能为空")@RequestParam("loginname") String loginname,
                           @Valid @NotBlank(message = "密码不能为空")@RequestParam("pwd") String pwd,
                           @Valid @NotBlank(message = "电话不能为空")@RequestParam("tel") String tel,
                           @Valid @NotBlank(message = "地址不能为空")@RequestParam("address") String address){
        if  (!new methods().checklenth(nike,3,10)) {return new Result(400,"昵称长度不正确");}
        if  (!new methods().checklenth(loginname,7,15)) {return new Result(400,"用户名长度不正确");}
        if  (!new methods().checklenth(pwd,6,15)) {return new Result(400,"密码长度不正确");}
        int count = userService.count(new QueryWrapper<User>().eq("loginname",loginname));
        if (count != 0){
            return new Result(400,"注册账号已经存在！");
        }
        User user = new User();
        user.setNike(nike);
        user.setLoginname(loginname);
        user.setPwd(new methods().md5(pwd));
        user.setTel(tel);
        user.setAddress(address);
        user.setToken(new methods().randomstr(14));
        boolean isSave = userService.save(user);
        if (isSave){return new Result(200,"注册成功");}
        else{return new Result(400,"注册失败");}
    }

    @GetMapping("/usergetdata")
    public Result usergetdata(@RequestParam("token") String token,
                              @RequestParam("id") String id){
        return new Result(200,"get",userService.getOne(new QueryWrapper<User>().eq("id",id)));
    }

    /**
     * 更新用户地址
     * @param id
     * @param address
     * @return
     */
    @PostMapping("/updateUserAddress")
    public Result updateUserAddress(@Valid @NotBlank(message = "id不能为空") @RequestParam("id") String id,
                                    @Valid @NotBlank(message = "地址不能为空") @RequestParam("address") String address){
        boolean update = userService.update(new UpdateWrapper<User>().set("address", address).eq("id", id));
        if (update) {
            return new Result(200, "更新成功");
        }else{
            return new Result(400,"更新失败");
        }
    }

    /**
     * 更新用户电话
     * @param id
     * @param tel
     * @return
     */
    @PostMapping("/updateUserTel")
    public Result updateUserTel(@Valid @NotBlank(message = "id不能为空")@RequestParam("id") String id,
                                @Valid @NotBlank(message = "电话不能为空")@RequestParam("tel") String tel){
        boolean update = userService.update(new UpdateWrapper<User>().set("tel", tel).eq("id", id));
        if (update) {
            return new Result(200, "更新成功");
        }else{
            return new Result(400,"更新失败");
        }
    }
}
